package com.yxy.springcloud.framework.unionauth.utils;

import com.alibaba.fastjson.JSON;
import com.yxy.springcloud.framework.common.base.BusinessException;
import com.yxy.springcloud.framework.common.redis.RedisUtils;
import com.yxy.springcloud.framework.unionauth.model.AuthAdminUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class TokenCheckUtil {
  @Autowired
  private RedisUtils redisUtils;

  //存储 不需要验证token的url列表
  private static List<String> noAuthUrls = new ArrayList<>();

  static {
    noAuthUrls.add("/login/in");
    noAuthUrls.add("/app-login/in");
    noAuthUrls.add("/app-login/loginWithCheckCode");
    noAuthUrls.add("/app/list");
    noAuthUrls.add("/router/save");
    noAuthUrls.add("/tenant/list");
    noAuthUrls.add("/tenant/getTenantById");
    noAuthUrls.add("/tenant/createOrUpdate2");
    noAuthUrls.add("/checkCode/get");
    noAuthUrls.add("/data-dic/getDataDicByApp");
    noAuthUrls.add("/data-dic/getDataDicDetailById");
    noAuthUrls.add("/data-dic/getDataDicDetailByValue");
    noAuthUrls.add("/app-system-setting/getParamValue");
    noAuthUrls.add("/app-user/createOrUpdate2");
    noAuthUrls.add("/app-user/deleteFromApp");
  }

  //根据token获取登陆用户信息
  public String getUserInfoByToken() throws BusinessException {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    String requestURI = request.getRequestURI();
    System.out.println("requestURI---->" + requestURI);
    // 放行某些url,不验证token
    if (noAuthUrls.contains(requestURI)) {
      return null;
    }
    String token = request.getHeader("X-Token");
    if (StringUtils.isEmpty(token)) {
      throw new BusinessException(403, "token不能为空");
    }

    String adminUserStr = redisUtils.get(token);
    if (adminUserStr == null) {
      throw new BusinessException(403, "token无效");
    }
    // AuthAdminUser adminUser = JSON.parseObject(adminUserStr, AuthAdminUser.class);
    return adminUserStr;
  }

  // 验证app后台直接调用union auth接口是否合法
  public void checkAppPermission() throws BusinessException {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    String invokeUnionAuthToken = request.getParameter("invoke_union_auth_token");
    System.out.println("invokeUnionAuthToken---->" + invokeUnionAuthToken);

    if (StringUtils.isEmpty(invokeUnionAuthToken)) {
      throw new BusinessException(403, "app后台调用union auth接口的token不能为空");
    }

    String invokeUnionAuthToken2 = redisUtils.get("invoke_union_auth_token");
    if (!invokeUnionAuthToken.equals(invokeUnionAuthToken2)) {
      throw new BusinessException(403, "app后台调用union auth接口的token无效");
    }
  }

  //将token保存在后端内存中(redis中)
  public void pushToken(String token, Object adminUser) {
    String adminUserStr = JSON.toJSONString(adminUser);
    int expireTime = 60 * 60 * 24; // token自动失效时间,单位 s
    redisUtils.setEx(token, adminUserStr, expireTime);
  }

  /**
   * 将token保存在后端内存中(redis), 指定过期时间
   * @param token
   * @param adminUser
   * @param expireTime token自动失效时间,单位 s
   */
  public void pushToken(String token, Object adminUser, int expireTime) {
    String adminUserStr = JSON.toJSONString(adminUser);
    redisUtils.setEx(token, adminUserStr, expireTime);
  }

  public void deleteToken(String token) {
    redisUtils.del(token);
  }
}
